package org.smart4j.util;

/**
 * Created by liquan on 2017/6/12.
 */
public class CaseUtil {
    public static String caseString(Object obj) {
        return caseString(obj, "");
    }

    public static String caseString(Object obj, String defaultValue) {
        String value = defaultValue;
        if (obj != null) {
            value = String.valueOf(obj);

        }
        return value;
    }

    public static double caseDouble(Object obj) {
        return caseDouble(obj, 0);
    }

    public static double caseDouble(Object obj, double defaultValue) {
        double value = defaultValue;
        if (obj != null) {
            String strValue = caseString(obj);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    value = Double.parseDouble(strValue);

                } catch (NumberFormatException e) {
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    public static int caseInt(Object obj) {
        return caseInt(obj, 0);
    }

    public static int caseInt(Object obj, int defaultValue) {
        int value = defaultValue;
        if (obj != null) {
            String strValue = caseString(obj);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    value = Integer.parseInt(strValue);
                } catch (NumberFormatException e) {
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    public static long caseLong(Object obj) {
        return caseLong(obj, 0);
    }

    public static long caseLong(Object obj, long defaultValue) {
        long value = defaultValue;
        if (obj != null) {
            String strValue = caseString(obj);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    value = Long.parseLong(strValue);
                } catch (NumberFormatException e) {
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    public static boolean caseBoolean(Object obj) {
        return caseBoolean(obj, false);
    }

    public static boolean caseBoolean(Object obj, boolean defaultValue) {
        boolean value = defaultValue;
        if (obj != null) {
            value = Boolean.parseBoolean(caseString(obj));
        }
        return value;
    }
}
